package com.example.rocketmq.base.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @DESCRIPTION:
 * @USER: shg
 * @DATE: 2023/3/9 16:14
 */
public class Consumer {

    public static void main(String[] args) throws Exception {
        // 1.创建消费者Consumer，制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");

        // 2.指定Nameserver地址
        consumer.setNamesrvAddr("192.168.11.129:9876;192.168.11.130:9876");
        // producer.setNamesrvAddr("192.168.2.50:9876;192.168.2.51:9876");

        // 3.订阅主题Topic和Tag
        consumer.subscribe("base", "Tag3");

        // 4.设置回调函数，处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msg, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.println(msg);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        // 5.启动消费者consumer
        consumer.start();
    }
}
